package Easy合并链表;

import org.testng.annotations.Test;

public class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2){

        ListNode read = new ListNode(0,l1);
        ListNode tail = new ListNode(0,l1);
        ListNode head = tail;
        read=read.next;

        while ( l2 != null) {
            int n2 = l2.val;
            while (read != null) {
                if(read.val < n2 ) {
                    read = read.next;
                    tail = tail.next;
                }
                else {
                    ListNode num = new ListNode();
                    num.val = n2;
                    num.next = tail.next;
                    tail.next = num;
                    tail = tail.next;
                    break;
                }
            }
            l2 = l2.next;
        }

        return head.next;
    }

    @Test
    public void test(){
        ListNode l1 = new ListNode(0,new ListNode(2,new ListNode(4,new ListNode(5,null))));
        ListNode l2 = new ListNode(1,new ListNode(3,new ListNode(4,null)));

        ListNode T1 = mergeTwoLists(l1,l2);

        while(T1 != null){
            System.out.println(T1.val);
            T1 = T1.next;
        }
    }
}
